/**
 * 
 */
package grid;

import alg.Evaluation;
import util.Utils;
import java.util.*;
import org.jgap.Configuration;
import org.jgap.Genotype;
import org.jgap.IChromosome;
import util.GridConf;
import static grid.Constants.*;

/**
 * @author Darko
 * @author Dragan Sahpaski
 * 
 */
public class Migrator {

    private static int migrationWidth;
    private static int epoch;
    private static int noOfInputs;

    private static void setup(String[] args) {
        if (args.length < 3) {
            throw new IllegalArgumentException();
        }
        epoch = Integer.parseInt(args[0]);
        migrationWidth = Integer.parseInt(args[1]);
        if (migrationWidth < 0) {
            throw new IllegalArgumentException();
        }
        noOfInputs = Integer.parseInt(args[2]);
        if (noOfInputs <= 0) {
            throw new IllegalArgumentException();
        }

    }

    public static void main(String[] args) throws Exception {
        setup(args);
        StringBuffer log = new StringBuffer("migrator: start:" + new Date());
        Configuration conf = Utils.getDefaultConfiguration(
                20, new Evaluation(GridConf.getDwConfiguration()));
        List<Genotype> populations = new ArrayList<Genotype>();
        List<IChromosome> bestChromosomes = new ArrayList<IChromosome>();

        List<String> breederLogs = new ArrayList<String>();
        for (int i = 0; i < noOfInputs; i++) {
            String populationFile = "population" + (i + 1) + "_" + epoch + OUTPUT_POSTFIX;
            String logFile = "breeder" + (i + 1) + "_" + epoch + LOG_POSTFIX;
            populations.add(Utils.loadGenotypeFromGZIP(populationFile, conf));
            breederLogs.add(Utils.loadStringFromGZIP(logFile));
            bestChromosomes.add(populations.get(i).getFittestChromosome());
        }

        // Give material to the previous and to the next process
        Random r = new Random(System.currentTimeMillis());
        if (populations.size() > 1) {
            for (int i = 0; i < populations.size(); i++) {
                // Exchange material
                Genotype population = populations.get(i);
                population.getPopulation().setChromosome(
                        r.nextInt(populations.size() - 1), bestChromosomes.get((i - 1) != -1 ? (i - 1) : populations.size() - 1));
                population.getPopulation().setChromosome(
                        r.nextInt(populations.size() - 1), bestChromosomes.get((i + 1) != populations.size() ? (i + 1) : 0));
                String outputFile = "population" + (i + 1) + "_" + (epoch + 1) + EXTENSION;
                Utils.saveGenotypeToGZIP(outputFile, population);
            }
        }

        // The summary is the first row
        StringBuilder breederSummaries = new StringBuilder();
        StringBuilder breedersLogs = new StringBuilder();
        for (String breederLog : breederLogs) {
            breederSummaries.append("\n" + breederLog.split("\n")[0]);
            breedersLogs.append(breederLog + "\n\n");
        }
        log.append(" end:" + new Date());
        log.append("\n" + breederSummaries + "\n\n");
        log.append(breedersLogs);

        String logFile = "migrator" + (epoch + 1) + LOG_POSTFIX;
        Utils.saveStringToGZIP(logFile, log.toString());
    }
}
